<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_product extends CI_Controller {
    public $setting = '';
    private $allchild = array();
    private $per_page = 12;
    private $total_rows = 6;
    public $breacum;
    private $mang_idcat;
    private $all_cat_child=array();
    function  __construct(){
        parent::__construct();
        $this->setting =$this->common_model->getone('setting',array('id'=>1));
        
    }
    public function index(){
        //lay san pham noi bat- san pham moi -- sam pham ban chay
        $this->load->view('mod_product/tab_sp',array(
            'noibat'=>  $this->common_model->getdata('product',array('publish'=>1,'noibat'=>1),array('img','title','alias','price','tgbaohanh'),array('order','ASC'),4),
            'banchay'=> $this->common_model->getdata('product',array('publish'=>1,'banchay'=>1),array('img','title','alias','price','tgbaohanh'),array('order','ASC'),4),
            'new'=>     $this->common_model->getdata('product',array('publish'=>1),array('img','title','alias','price','tgbaohanh'),array('id','DESC'),4)
            )
        );        
        
    }
    function ajaxorder(){
        switch ($this->input->post('data')){
            case 1:
                $this->session->set_userdata('orderbycat',array('title','ASC'));
                break;
            case 2:
                $this->session->set_userdata('orderbycat',array('title','DESC'));
                break;
            case 3:
                $this->session->set_userdata('orderbycat',array('price','DESC'));
                break;
            case 4:
                $this->session->set_userdata('orderbycat',array('price','ASC'));
                break;
        }
    }
    function danhmucnb(){
        $catalog=$this->common_model->getdata('product_category',array('publish'=>1,'highlights'=>1),array('id','title','alias','image'),array('order','ASC'),4);
        if(!empty($catalog)){
            foreach ($catalog as $cat){
                $mm[]=$cat->id;
            }
        }
        $this->db->where_in('category',$mm);
        $product =$this->common_model->getdata('product',array('publish'=>1,'noibat'=>1),array('img','title','alias','price','category'),array('order','ASC'),'');
        if(!empty($product)){
            foreach($product as $pro){
                $mpro[$pro->category][]=$pro;
            }
        }else $mpro='';
        $this->load->view(
            'mod_product/danhmucnb',
            array(
                'data'=>$catalog,
                'product'=>$mpro
               )
               );
    }
    function spbanchay(){
        $this->load->view(
            'mod_product/spbanchay',
            array(
                'data'=>$this->common_model->getdata('product',array('publish'=>1,'banchay'=>1),array('img','title','alias','price','tgbaohanh'),array('order','ASC'),4)
            )
        );
    }
    function sptieubieu(){
        $this->load->view(
            'mod_product/sptieubieu',
            array(
                'data'=>$this->common_model->getdata('product',array('publish'=>1,'tieubieu'=>1),array('img','title','alias','price','tgbaohanh'),array('order','ASC'),12)
            )
        );
    }
    function catecreat($arrcate=array(),$id = 0){
        if(isset($arrcate[$id])){
            foreach ($arrcate[$id] as $onecate ){
                $this->allchild[] = $onecate->id;
                $this->all_cat_child=$onecate;
                if(!empty($arrcate[$onecate->id])){
                    $this->catecreat($arrcate,$onecate->id);
                }
            }
            return $this->allchild;
        }else{
            return array();
        }
    }
    //danh muc san pham/////////////////////////////////////
    function category($alias){
            $alias =trim(preg_replace("/(.1.html)/", '', $alias));//cat duoi html
            $category = $this->common_model->getone('product_category',array('publish'=>1,'alias'=>$alias));                       
            if(!empty($category)){ 
             $offset = $this->uri->segment(2); 
            //lay cat id cua ca con
            $this->getcatid($category->id);  
            $this->mang_idcat[]=$category->id;// them vao id cua chinh no
            ///end lay catid cua con
            if($this->session->userdata('orderbycat')){
                $order=$this->session->userdata('orderbycat');
                $this->db->order_by($order[0],$order[1]);
            }else $this->db->order_by('id','DESC');
            $product = $this->common_model->getdatain('product',array('category',$this->mang_idcat),array(),array($this->per_page,$offset));             
            $this->total_rows = $this->common_model->getnumber('product',array('category',$this->mang_idcat));
            $this->phantrang(); 
            $data['content'] = $this->load->view('mod_product/category',array('product'=>$product,'category'=>$category,'childcat'=>$this->all_cat_child),true);            
            }else $data['content'] =  '';  
        $this->load->view('default/index',$data); 
        
    }    
    // lay id cua menu con
    function getcatid($id){
        // get all category ->menu
        $dat = $this->common_model->getdata('product_category',array('publish'=>1));
        if($dat){
            foreach ( $dat as $v){
                $data[$v->parentid][]=$v;
            }
            $this->menucreat($data,$id);        
        }   
    }
    function menucreat($data,$id=0){          
        if(!empty($data[$id])){
            foreach ($data[$id] as $dulieu ){ 
                $this->mang_idcat[] =$dulieu->id;
                if(!empty($data[$dulieu->id])){
                    $this->menucreat($data,$dulieu->id);
                }            
            }
        }
    }   
    // end menu con 
    
      
    function detail($alias){  
        $alias =trim(preg_replace("/(.1.html)/", '', $alias));//cat duoi html
        $product = $this->common_model->getone('product',array('publish'=>1,'alias'=>$alias));
        if(!empty($product)){
          $data['content'] = $this->load->view('mod_product/detail',array('product'=>$product),true);                    
          $this->breadcrumbs($product->category);
          $data['breadcrumbs'] = $this->breacum .' &raquo; <span>'.$product->title.'</span>';   
          $data['title'] = $product->title;
          $data['metades'] = $product->metades;
          $data['metakey'] = $product->metakey;
        }else{
            $data['title'] = $this->setting->title;
            $data['metades'] = $this->setting->metades;
            $data['metakey'] = $this->setting->metakey;
        }        
        
        $this->load->view('default/index',$data); 
    }    
    function tb_product(){
        $data['product'] = $this->common_model->getdata('product',array('publish'=>1,'tieubieu'=>1),array(),array('id','DESC'),3);
        $this->load->view('mod_product/tieubieu',$data);
    }
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = base_url().$this->uri->segment(1);
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 2;
        $config['num_links'] = 12;
        $this->pagination->initialize($config);
    }
    function breadcrumbs($id=0){
        $catget = $this->common_model->getone('product_category',array('id'=>$id));
        if(!empty($catget)){
            if(!empty($this->breacum)){
                $this->breacum ='<a href="'.base_url().'san-pham/'.$catget->alias.'">'.$catget->title.'</a> &raquo; '.$this->breacum;  
            }else $this->breacum ='<a href="'.base_url().'san-pham/'.$catget->alias.'">'.$catget->title.'</a>';
            if($catget->parentid!=0){
                $this->breadcrumbs($catget->parentid);               
                
            }
        }      
    }
    function ajax_locsanpham(){
        if(!empty($_POST)){            
            $uri3 = $this->security->xss_clean($this->uri->segment(3,0));//hien chu option
            $uri4 = $this->security->xss_clean($this->uri->segment(4,0));//hien id
            $uri5 = $this->security->xss_clean($this->uri->segment(5,0));//hien id//can lay them id cua danh muc de bo link
            $m_like = $this->session->userdata('like');
            $optionid = $this->session->userdata('optionid');
            if(is_numeric($uri4)){
                switch($uri3){
                case 'option':                                
                    $m_like[]=$uri4;
                    $optionid[]=$uri5;               
                    sort($m_like);
                    $this->session->set_userdata('like',$m_like);
                    $this->session->set_userdata('optionid',$optionid);
                    break;
                case 'removed':                
                    $optionid=array_diff($optionid, array($uri5));
                    $m_like=array_diff($m_like, array($uri4));
                    $this->session->set_userdata('like',$m_like);
                    $this->session->set_userdata('optionid',$optionid); 
                    break;
                case 'gia':
                    $this->session->set_userdata('gia',$uri4);
                    break; 
                case 'gia_removed':
                    $this->session->unset_userdata('gia');
                    break;             
            }
                
            }
        }
    }
}